var prefix = ctx+"/sys/menu"
$(function() {
	validateRule();
	//打开图标列表
    $("#ico-btn").click(function(){
        layer.open({
            type: 2,
			title:'图标列表',
            content: prefix+'/fontIcon',
            area: ['480px', '90%'],
            success: function(layero, index){
            }
        });
    });
});
$.validator.setDefaults({
	submitHandler : function() {
		submit01();
	}
});
function submit01() {
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/save",
		data : $('#signupForm').serialize(),
		async : false,
		error : function(request) {
			laryer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("保存成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				layer.alert(data.msg)
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			type : {
				required : true
			},
			orderNum : {
				required : true,
				digits:true
			}
		},
		messages : {
			name : {
				required : icon + "请输入菜单名"
			},
			type : {
				required : icon + "请选择菜单类型"
			},
			orderNum : {
				required : icon + "请输入排序",
				digits: icon + "请输入整数"
			}
		}
	})
}

var menuId,menuText;
function loadMenuTree(menuTree) {
	$('#menuTree').jstree({
		"plugins" : [ "wholerow","contextmenu"],
		'core' : {
			'data' : menuTree,
			"multiple" : false, // 单选
		}
	});
	$('#menuTree').jstree('open_all');
	$("#menuTree").on('changed.jstree', function (e, data) {
		var node = data.instance.get_node(data.selected[0]);
		menuId=node.id;
		menuText=node.text;
	});
}
function getMenuTreeData() {
	$.ajax({
		type : "GET",
		url : ctx+"/sys/menu/tree/",
		success : function(data) {
			loadMenuTree(data);
		}
	});
}
function openTree(){
	layer.open({
	  title: '选择上级菜单',
	  area: ['280px', '70%'],
	  content: '<div id="menuTree"></div>',
	  yes: function(index, layero){
		  console.log(menuId+" "+menuText);
		  $("#parentId").val(menuId);
		  $("#parentName").val(menuText);
		  layer.close(index);
	  }
	}); 
	getMenuTreeData();
}